package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.article.ArticleListener;
import com.prp.gwt.client.vo.Entity;
import com.prp.gwt.client.widgets.MessageDialogBox;

public class ArticleEditPanel extends VerticalPanel {

	private static ArticleEditPanel instance;
	private RichTextArea area = new RichTextArea();
	private TextBox title = new TextBox();
	private Entity entity;
	private HorizontalPanel hPanel = new HorizontalPanel();
	
	public static ArticleEditPanel getInstance() {
		if (instance == null)
			instance = new ArticleEditPanel();
		return instance;
	}

	private ArticleEditPanel() {
		addStyleName("addRecommend");
		{
			add(hPanel);
			HorizontalPanel panel = new HorizontalPanel();
			Label label= new Label("发表新的推荐");
			label.addStyleName("addRecommend-top-text");
			panel.add(label);
			HTML html = new HTML("<a>返回</a>");
			html.addStyleName("addRecommend-top-link");
			panel.add(html);
			html.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.back();
				}
			});
			add(panel);
		}
		VerticalPanel editingPanel = new VerticalPanel();
		editingPanel.addStyleName("addRecommend-body");
		HorizontalPanel tPanel = new HorizontalPanel();
		tPanel.addStyleName("addRecommend-body-title");
		HTML html = new HTML("标题:");
		html.addStyleName("addRecommend-body-title-text");
		tPanel.add(html);
		tPanel.add(title);
		editingPanel.add(tPanel);
		area.addStyleName("addRecommend-body-area");
		RichTextToolbar toolbar = new RichTextToolbar(area);
		toolbar.setWidth("100%");
		Grid grid = new Grid(2, 1);
		grid.setWidth("100%");
		grid.setStyleName("richText");
		grid.setWidget(0, 0, toolbar);
		grid.setWidget(1, 0, area);
		editingPanel.add(grid);
		Button commit = new Button("提交");
		commit.addStyleName("addRecommend-body-button");
		editingPanel.add(commit);
		add(editingPanel);
		commit.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(entity == null)
					MessageDialogBox.show("错误", "请等待服务器响应");
				int id = entity.getId();
				if (title.getText() == null || title.getText().isEmpty()) {
					MessageDialogBox.show("错误", "请输入标题");
				} else if (area.getHTML() == null || area.getHTML().isEmpty()) {
					MessageDialogBox.show("错误", "请输入正文");
				} else {
					String str = area.getHTML();
					if (str.length() > 20000)
						str = str.substring(0, 20000);
					ArticleListener.getInstance().createRecommend(id,
							title.getText(), str);
				}
			}
		});
	}

	public void init() {
		area.setHTML("");
		title.setText("");
	}

	public void load(Entity entity){
		this.entity = entity;
		hPanel.clear();
		HTML html  = new HTML("<a>"+entity.getCategoryName()+"</a>");
		hPanel.add(html);
		html.addStyleName("addRecommend-top-link");
		Label label = new Label(">");
		label.addStyleName("addRecommend-top-text");
		hPanel.add(label);
		html = new HTML("<a>"+entity.getChildCategoryName()+"</a>");
		html.addStyleName("addRecommend-top-link");
		hPanel.add(html);
		label = new Label(">");
		label.addStyleName("addRecommend-top-text");
		hPanel.add(label);
		html = new HTML("<a>"+entity.getName()+"</a>");
		html.addStyleName("addRecommend-top-link");
		hPanel.add(html);
	}
	
}
